<html>
<head><meta charset="utf-8"><title>repr-attribute is allowed in fields · t-lang · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/index.html">t-lang</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html">repr-attribute is allowed in fields</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="214440745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/repr-attribute%20is%20allowed%20in%20fields/near/214440745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html#214440745">(Oct 24 2020 at 18:01)</a>:</h4>
<p>Turns out this is accepted by the compiler:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Ascii</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">inner</span>: <span class="kt">u8</span> <span class="p">};</span><span class="w"></span>
<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span>: <span class="kp">&amp;</span><span class="nc">Ascii</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">Ascii</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="cp">#[repr(transparent)]</span><span class="w"> </span><span class="n">inner</span>: <span class="mi">1</span><span class="k">u8</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>

<p>Even though of course a <code>repr</code> attribute on a field currently has no semantics. This is unfortunate as this might be an interesting way to safely in-place construct such a wrapper type, i.e. a method <code>&amp;mut u8 -&gt; Option&lt;&amp;mut Ascii&gt;</code>.<br>
In the grander scope, are _all_ attributes currently ignored on fields? If not, where is it documented how they are interpreted?</p>



<a name="214443043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/repr-attribute%20is%20allowed%20in%20fields/near/214443043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html#214443043">(Oct 24 2020 at 18:49)</a>:</h4>
<p>There's been work to deny attributes in more places (e.g.https://github.com/rust-lang/rust/pull/77015)</p>



<a name="214443054"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/repr-attribute%20is%20allowed%20in%20fields/near/214443054" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html#214443054">(Oct 24 2020 at 18:49)</a>:</h4>
<p>I think that could easily be extended to deny <code>#[repr(transparent)]</code> on non-items</p>



<a name="214444021"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/repr-attribute%20is%20allowed%20in%20fields/near/214444021" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> HeroicKatora <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html#214444021">(Oct 24 2020 at 19:09)</a>:</h4>
<p>An inner attribute <code>#![repr(transparent)]</code> expresses the intent better here and is not allowed so it's not a blocker for that syntax (drafting RFC right now). But still something to be aware of and probably good to deny for forward compatibility.</p>



<a name="214462703"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/213817-t-lang/topic/repr-attribute%20is%20allowed%20in%20fields/near/214462703" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/213817-t-lang/topic/repr-attribute.20is.20allowed.20in.20fields.html#214462703">(Oct 25 2020 at 02:17)</a>:</h4>
<p>Sadly the automatic <code>unused_attribute</code> lint seems unreliable at best, so attribute authors need to remember to check for misplaced ones, which is often missed in certain positions.</p>
<p>PRs to improve that are always appreciated, though whether we're (T-lang) willing to make them hard errors tends to depend on ecosystem impact.  (One of these was just discussed at a recent triage meeting.)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>